package com.paishen.peiniwan.module;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMChatManager;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.module.date.MainDateLayout;
import com.paishen.peiniwan.module.message.MainMessageLayout;
import com.paishen.peiniwan.module.profile.MainProfileLayout;
import com.paishen.peiniwan.module.square.MainSquareLayout;

public class MainLayout extends LinearLayout {
    private final MainProfileLayout layMainProfile;
    private final MainSquareLayout layMainSquare;
    private final MainDateLayout layMainDate;
    private final MainMessageLayout layMainMessage;

    private final ImageView imgSquare, imgDate, imgMessage, imgProfile;
    private final TextView txSquare, txDate, txMessage, txProfile;
    private final View viewNewDate, viewNewMessage;

    private long currOpenedMenuId;

    public MainLayout(Context context) {
        this(context, null);
    }

    public void setOnProfileMenuClickListener(MainProfileLayout.OnProfileMenuClickListener callback) {
        layMainProfile.setOnProfileMenuClickListener(callback);
    }

    private OnBottomTabClickListener callback;

    public interface OnBottomTabClickListener {
        void onSquareClick(int menuId);

        void onDateClick(int menuId);

        void onCenterClick(int menuId);

        void onMessageClick(int menuId);

        void onProfileClick(int menuId);
    }

    public void setOnBottomTabClickListener(OnBottomTabClickListener callback) {
        this.callback = callback;
    }


    public void dealBottomMenuClick(int menuId) {
        layMainSquare.stopAnimation();

        currOpenedMenuId = menuId;
        switch (menuId) {
            case R.id.lay_square:
                imgSquare.setImageResource(R.mipmap.main_square_selected);
                imgDate.setImageResource(R.mipmap.main_date_normal);
                imgMessage.setImageResource(R.mipmap.main_message_normal);
                imgProfile.setImageResource(R.mipmap.main_profile_normal);
                txSquare.setTextColor(0xFFFC4E52);
                txDate.setTextColor(0xFFB3B3B3);
                txMessage.setTextColor(0xFFB3B3B3);
                txProfile.setTextColor(0xFFB3B3B3);
                layMainSquare.setVisibility(View.VISIBLE);
                layMainSquare.startAnimation();
                layMainDate.setVisibility(View.GONE);
                layMainMessage.setVisibility(View.GONE);
                layMainProfile.setVisibility(View.GONE);
                break;
            case R.id.lay_date:
                imgSquare.setImageResource(R.mipmap.main_square_normal);
                imgDate.setImageResource(R.mipmap.main_date_selected);
                imgMessage.setImageResource(R.mipmap.main_message_normal);
                imgProfile.setImageResource(R.mipmap.main_profile_normal);
                txSquare.setTextColor(0xFFB3B3B3);
                txDate.setTextColor(0xFFFC4E52);
                txMessage.setTextColor(0xFFB3B3B3);
                txProfile.setTextColor(0xFFB3B3B3);
                layMainSquare.setVisibility(View.GONE);
                layMainDate.setVisibility(View.VISIBLE);
                layMainMessage.setVisibility(View.GONE);
                layMainProfile.setVisibility(View.GONE);
                break;
            case R.id.lay_message:
                imgSquare.setImageResource(R.mipmap.main_square_normal);
                imgDate.setImageResource(R.mipmap.main_date_normal);
                imgMessage.setImageResource(R.mipmap.main_message_selected);
                imgProfile.setImageResource(R.mipmap.main_profile_normal);
                txSquare.setTextColor(0xFFB3B3B3);
                txDate.setTextColor(0xFFB3B3B3);
                txMessage.setTextColor(0xFFFC4E52);
                txProfile.setTextColor(0xFFB3B3B3);
                layMainSquare.setVisibility(View.GONE);
                layMainDate.setVisibility(View.GONE);
                layMainMessage.setVisibility(View.VISIBLE);
                layMainProfile.setVisibility(View.GONE);
                break;
            case R.id.lay_profile:
                imgSquare.setImageResource(R.mipmap.main_square_normal);
                imgDate.setImageResource(R.mipmap.main_date_normal);
                imgMessage.setImageResource(R.mipmap.main_message_normal);
                imgProfile.setImageResource(R.mipmap.main_profile_selected);
                txSquare.setTextColor(0xFFB3B3B3);
                txDate.setTextColor(0xFFB3B3B3);
                txMessage.setTextColor(0xFFB3B3B3);
                txProfile.setTextColor(0xFFFC4E52);
                layMainSquare.setVisibility(View.GONE);
                layMainDate.setVisibility(View.GONE);
                layMainMessage.setVisibility(View.GONE);
                layMainProfile.setVisibility(View.VISIBLE);
                break;
        }
    }

    public MainLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_main, this);

        imgSquare = (ImageView) findViewById(R.id.img_square);
        imgDate = (ImageView) findViewById(R.id.img_date);
        imgMessage = (ImageView) findViewById(R.id.img_message);
        imgProfile = (ImageView) findViewById(R.id.img_profile);
        txSquare = (TextView) findViewById(R.id.tx_square);
        txDate = (TextView) findViewById(R.id.tx_date);
        txMessage = (TextView) findViewById(R.id.tx_message);

        txProfile = (TextView) findViewById(R.id.tx_profile);
        final View.OnClickListener onMenuClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    // 把事件传递回MainActivity Profile 菜单去频繁点击处理
                    switch (v.getId()) {
                        case R.id.lay_square:
                            callback.onSquareClick(R.id.lay_square);
                            break;
                        case R.id.lay_date:
                            if (View.VISIBLE == viewNewDate.getVisibility()) {
                                viewNewDate.setVisibility(View.GONE);
                            }
                            // SpfsUtil.write(C.sp.PUSH_DATE_NEW_DATE, false);
                            callback.onDateClick(R.id.lay_date);
                            break;
                        case R.id.lay_center:
                            callback.onCenterClick(R.id.lay_center);
                            break;
                        case R.id.lay_message:
                            callback.onMessageClick(R.id.lay_message);
                            break;
                        case R.id.lay_profile:
                            callback.onProfileClick(R.id.lay_profile);
                            layMainProfile.initData(true);
                            break;
                    }
                }
            }
        };

        findViewById(R.id.lay_square).setOnClickListener(onMenuClickListener);
        findViewById(R.id.lay_date).setOnClickListener(onMenuClickListener);
        findViewById(R.id.lay_center).setOnClickListener(onMenuClickListener);
        findViewById(R.id.lay_message).setOnClickListener(onMenuClickListener);
        findViewById(R.id.lay_profile).setOnClickListener(onMenuClickListener);


        ViewGroup layContent = (ViewGroup) findViewById(R.id.lay_content);
        layMainSquare = new MainSquareLayout(getContext());
        layMainSquare.setVisibility(View.VISIBLE);
        layMainDate = new MainDateLayout(getContext());
        layMainDate.setVisibility(View.GONE);
        layMainMessage = new MainMessageLayout(getContext());
        layMainMessage.setVisibility(View.GONE);
        layMainProfile = new MainProfileLayout(getContext());
        layMainProfile.setVisibility(View.GONE);

        layContent.addView(layMainProfile);
        layContent.addView(layMainDate);
        layContent.addView(layMainMessage);
        layContent.addView(layMainSquare);

        viewNewDate = findViewById(R.id.view_new_date);
        viewNewMessage = findViewById(R.id.view_new_message);
    }

    public void showSquareLayoutDialog() {
        layMainSquare.showSquareFilterDialog();
    }

    public void requestSquareIndex() {
        layMainSquare.requestSquareIndex();
    }

    public void initProfileMessage() {
        int unreadMsgCountTotal = 0;
        int chatroomUnreadMsgCount = 0;
        EMChatManager chatManager = EMClient.getInstance().chatManager();
        unreadMsgCountTotal = chatManager.getUnreadMsgsCount();
        for (EMConversation conversation : chatManager.getAllConversations().values()) {
            if (conversation.getType() == EMConversation.EMConversationType.ChatRoom)
                chatroomUnreadMsgCount = chatroomUnreadMsgCount + conversation.getUnreadMsgCount();
        }
        int unreadTotal = unreadMsgCountTotal - chatroomUnreadMsgCount;
        viewNewMessage.setVisibility(unreadTotal > 0 ? View.VISIBLE : View.INVISIBLE);

        layMainMessage.refresh();
    }

    public void initProfileInfo(boolean byNewRequest) {
        layMainProfile.initData(byNewRequest);
    }

    public void initProfileAlbumPreview() {
        layMainProfile.initAlbumPhoto(false);
    }

    public void refreshDate(boolean hasNewDate) {
        if (hasNewDate) {
            if (currOpenedMenuId != R.id.lay_date) {
                viewNewDate.setVisibility(View.VISIBLE);
            }
            layMainDate.requestOrderGetBookPending();
        } else {
            viewNewDate.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        layMainSquare.stopAnimation();
    }
}
